package simulation;

import java.awt.Color;
import java.awt.Dimension;
import util.Vector;


/**
 * Muscle extends Spring.
 * has length varying with a sin function
 *
 * @author Schwartz & Roy
 */
public class Muscle extends Spring {
    // muscle properties
    private static double TIME_SINCE_CREATION;
    private double myAmplitude;
    private double myOriginalLength;

    /**
     * constructor for Muscle class.
     * @param start is one connecting Mass
     * @param end is other connecting Mass
     * @param length is initial length of Muscle
     * @param kVal is k constant
     * @param amplitude is amplitude of the sin wave
     */
    public Muscle(Mass start, Mass end, double length,
            double kVal, double amplitude) {
        super(start, end, length, kVal);
        myOriginalLength = length;
        myAmplitude = amplitude;
    }

    /**
     * muscles are green to differentiate from springs.
     * @param diff
     */
    @Override
    protected Color getColor(double diff) {
        if (Vector.fuzzyEquals(diff, 0)) {
            return Color.BLACK;
        } else if (diff < 0.0) {
            return Color.GREEN;
        } else {
            return Color.GREEN;
        }
    }

    /**
     * calls method to update the muscle length based on time
     *      & calls super method.
     *
     * @param elapsedTime is time since last update
     * @param bounds of simulation
     */
    public void update(double elapsedTime, Dimension bounds) {
        updateLength();
        super.update(elapsedTime, bounds);
    }

    /**
     * updates the muscle length based on sin wave & time.
     */
    public void updateLength() {
        setLength((myAmplitude * Math.sin(TIME_SINCE_CREATION))
                * myOriginalLength);
    }

    /**
     * Increments the time for all the muscle instances.
     * @param elapsedTime
     */
    public static void incrementTime(double elapsedTime) {
        TIME_SINCE_CREATION += elapsedTime;
    }
}
